package com.lxs;

import javax.naming.directory.Attributes;
import javax.naming.directory.InitialDirContext;
import javax.naming.NamingException;
import java.util.Hashtable;

public class DNSLookup {
    public static void main(String[] args) {
        performDNSLookup();
    }

    public static void performDNSLookup() {
        String[] domains = {"google.com", "baidu.com", "localhost", "router"};

        for (String domain : domains) {
            try {
                System.out.println("查询域名: " + domain);
                lookupDNS(domain);
            } catch (NamingException e) {
                System.out.println("  查询失败: " + e.getMessage());
            }
            System.out.println();
        }
    }

    private static void lookupDNS(String domain) throws NamingException {
        Hashtable<String, String> env = new Hashtable<>();
        env.put("java.naming.factory.initial", "com.sun.jndi.dns.DnsContextFactory");

        InitialDirContext context = new InitialDirContext(env);
        Attributes attributes = context.getAttributes(domain, new String[]{"A"});

        System.out.println("  DNS记录: " + attributes);
    }
}
